<template>
  <div>
    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <div slot="header">
        <el-button type="primary" @click="addItem">新增</el-button>
      </div>
      <!-- card body -->
      <el-table :data="bannerList" border style="width: 100%">
        <el-table-column label="#" type="index" width="50" align="center">
        </el-table-column>
        <el-table-column label="广告图片" width="180" align="center">
          <template slot-scope="scope">
            <img class="img" :src="scope.row.pic" alt="" />
          </template>
        </el-table-column>
        <el-table-column label="活动时间" width="180" align="center">
          <template slot-scope="scope">
            开始时间:
            <div>{{ scope.row.startTime }}</div>
            结束时间:
            <div>{{ scope.row.endTime }}</div>
          </template>
        </el-table-column>
        <el-table-column label="广告名称" prop="name" align="center">
        </el-table-column>
        <el-table-column label="点击数" prop="clickCount" align="center">
        </el-table-column>
        <el-table-column label="下单数" prop="orderCount" align="center">
        </el-table-column>

        <el-table-column label="轮播位置" align="center">
          <template slot-scope="scope">
            <div>{{ scope.row.type ? "" : "首页轮播" }}</div>
          </template>
        </el-table-column>
        <el-table-column label="链接地址" prop="url" align="center">
        </el-table-column>
        <el-table-column label="备注" prop="note" align="center">
        </el-table-column>
        <el-table-column label="上下线状态" align="center">
          <template slot-scope="scope">
            <div>{{ scope.row.status ? "上线" : "下线" }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope" class="rightButton">
            <el-button type="primary" size="mini" @click="editItem(scope.row)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" @click="delRow(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 弹窗新增项目 -->
    <el-dialog
      title="广告详情"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose"
    >
      <el-form
        :model="bannerForm"
        ref="form"
        label-width="100px"
        :inline="false"
        size="normal"
      >
        <el-row :gutter="20">
          <el-col :span="10"
            ><el-form-item label="广告名称">
              <el-input v-model="bannerForm.name"></el-input> </el-form-item
          ></el-col>
          <el-col :span="10" :offset="2"
            ><el-form-item label="备注">
              <el-input v-model="bannerForm.note"></el-input> </el-form-item
          ></el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="10"
            ><el-form-item label="排序">
              <el-input v-model="bannerForm.sort"></el-input> </el-form-item
          ></el-col>
          <el-col :span="10" :offset="2"
            ><el-form-item label="产品数量">
              <el-input
                v-model="bannerForm.orderCount"
              ></el-input> </el-form-item
          ></el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="10"
            ><el-form-item label="链接">
              <el-input v-model="bannerForm.url"></el-input> </el-form-item
          ></el-col>
          <el-col :span="10" :offset="2"
            ><el-form-item label="首页轮播位置">
              <el-input v-model="bannerForm.type"></el-input> </el-form-item
          ></el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="10"
            ><el-form-item label="开始时间">
              <el-date-picker
                @blur="startTimeBlur"
                v-model="bannerForm.startTime"
                type="datetime"
                placeholder="选择日期时间"
              ></el-date-picker> </el-form-item
          ></el-col>
          <el-col :span="10" :offset="2"
            ><el-form-item label="结束时间">
              <el-date-picker
                @blur="endTimeBlur"
                v-model="bannerForm.endTime"
                type="datetime"
                placeholder="选择日期时间"
              ></el-date-picker> </el-form-item
          ></el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="10"
            ><el-form-item label="上下线状态">
              <el-switch
                v-model="bannerForm.status"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch> </el-form-item
          ></el-col>
          <el-col :span="12" :offset="2">
            <el-form-item label="图片">
              <el-upload
                v-if="!bannerForm.pic"
                action="/lejuAdmin/material/uploadFileOss"
                list-type="picture-card"
                :on-success="fileSuccess"
                :headers="{ token: fileToken }"
              >
                <i class="el-icon-plus"></i>
              </el-upload>
              <div v-else class="maskImg">
                <img class="img-c" :src="bannerForm.pic" alt="" />
                <i class="el-icon-close" @click="maskImgDel"></i>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeMask">取 消</el-button>
        <el-button type="primary" @click="createItem">创 建</el-button>
      </span>
    </el-dialog>

    <!-- 底部广告组件 -->
    <buttomBanner></buttomBanner>
  </div>
</template>

<script>
import buttomBanner from "@/components/buttomBanner";
import {
  adsList,
  addAds,
  delAds,
  updateAds,
} from "@/api/marketing/banner/banner.js";
import { getToken } from "@/utils/auth.js";
export default {
  data() {
    return {
      bannerList: [],
      dialogVisible: false,
      bannerForm: {
        name: "",
        note: "",
        orderCount: "",
        pic: "",
        sort: "",
        startTime: "",
        endTime: "",
        status: 0,
        type: 0,
      },
    };
  },
  computed: {
    // 上传图片携带token信息
    fileToken() {
      return getToken();
    },
  },
  methods: {
    // 重新编辑当前项
    editItem(item) {
      this.dialogVisible = true;
      this.bannerForm = item;
      console.log(this.bannerForm);
    },
    // 删除当前列表项
    delRow(item) {
      var id = item.id;
      this.$confirm("是否取消创建?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delAds(id).then((res) => {
            if (res.success) {
              this.$message.success("删除成功");
              this.init();
            } else {
              this.$message.error(res.message);
            }
          });
        })
        .catch(() => {});
    },
    // 获取选择开始时间
    startTimeBlur(a) {
      this.bannerForm.startTime = a.displayValue;
    },
    // 获取选择结束时间
    endTimeBlur(a) {
      this.bannerForm.endTime = a.displayValue;
    },
    // 上传图片删除
    maskImgDel() {
      this.$confirm("此操作将删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.bannerForm.pic = "";
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 上传图片
    fileSuccess(file) {
      if (file.success) {
        this.bannerForm.pic = file.data.fileUrl;
      }
    },
    init() {
      adsList().then((res) => {
        if (res.success) {
          this.bannerList = res.data.items;
        } else {
          this.$message.error(res.message);
        }
      });
    },
    // 显示新增窗口
    addItem() {
      this.dialogVisible = true;
    },
    // 新增弹窗关闭提示
    handleClose() {
      this.$confirm("是否取消创建?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.dialogVisible = false;
          this.$message({
            type: "success",
            message: "取消成功!",
          });
        })
        .catch(() => {});
    },
    // 弹窗取消按钮
    closeMask() {
      this.handleClose();
    },
    // 弹窗创建提交按钮
    createItem() {
      console.log(this.bannerForm);
      var ads = this.bannerForm;
      var api;
      var mes = "";
      if (this.bannerForm.id) {
        var api = updateAds;
        mes = "更新成功";
      } else {
        var api = addAds;
        mes = "创建成功";
      }
      api(ads).then((res) => {
        if (res.success) {
          this.dialogVisible = false;
          this.init();
          this.$message.success(mes);
        } else {
          this.$message.error(res.message);
        }
      });
    },
  },
  // 底部广告组件
  components: {
    buttomBanner,
  },
  created() {
    this.init();
  },
};
</script>

<style scoped lang="scss">
.img {
  width: 100px;
  height: 100px;
}
.rightButton {
  display: flex;
}
.maskImg {
  position: relative;
  width: 150px;
  height: 150px;
  .img-c {
    width: 100%;
    height: 100%;
  }
  .el-icon-close {
    position: absolute;
    right: 0;
    top: 0;
    color: rgb(238, 40, 14);
    font-size: 25px;
  }
}
</style>